import Vue from 'vue'

import 'normalize.css/normalize.css' // A modern alternative to CSS resets

import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import locale from 'element-ui/lib/locale/lang/zh-CN' // lang i18n

import '@/styles/index.scss' // global css

import App from './App'
import router from './router'
import store from './store';
import Config from './config';
import './api'

import {$config, $service, Service} from 'vue-element-simple';
import { getSaaSId } from './utils/auth'
// 重写请求方法, 加入mallId
Service.getService = async function (url, params, options = {}) {
  params.mallId = getSaaSId() || '';
  return await this.getSimpleService(`${$config.getApiPath()}${url}`, params, options);
};
// 接口地址
const API_PATH = `${Config['API_TYPE'] === 'prod' ? Config['API_PATH'] : Config['API_PATH_DEV']}`;
$config.setConfig({
  // 运行环境 LOCAL, CLOUD
  ENV: 'CLOUD',
// 接口请求地址
  API_PATH: API_PATH,
  // 请求头部
  API_HEADER: {
    'Content-Type': 'application/x-www-form-urlencoded'
  },
  // 上传地址
  UPLOAD_PATH: `${API_PATH}${Config['UPLOAD_PATH']}`,
  // 单个文件上传地址
  UPLOAD_SINGLE_FILE_PATH: `${API_PATH}/admin/file/upload`,
  // 请求公共参数
  REQUEST_COMMON_PARAMS: {
    // 请求混入公共参数
    token: () => $service.getToken(),
    mallId: getSaaSId()
  },

  // 获取历史图片列表方法
  'HISTORY_IMG_LIST_METHOD': async (page, size) => {
    const result = await $service.adminImageGetHistoryList({page: page, pageSize: size});
    if (result.code === 0) {
      const {list, count} = result.data;
      const arr = [];
      for (let i = 0; i < list.length; i++) {
        const {id, name, url} = list[i];
        arr.push({id, name, url});
      }
      return {list: arr, total: count};
    }
    return {list: [], total: 0};
  },

  // 删除历史图片方法
  'HISTORY_IMG_DEL_METHOD': async (list) => {
    const arr = [];
    for (let i = 0; i < list.length; i++) {
      arr.push(list[i]['id']);
    }
    const result = await $service.adminImageDelHistory({ids: arr.join(',')});
    return result.code === 0;
  }
});

import '@/icons' // icon

Vue.use(ElementUI, {locale});

Vue.config.productionTip = false;

new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App)
});
